<!-- 无感大转盘 -->
<template>
  <ele-pro-table
    ref="table"
    size="mini"
    :columns="columns"
    :datasource="datasource"
    :selection.sync="selection"
    :need-page="true"
    :hideOnSinglePage="false"
  >
    <!--头部按钮-->
    <template v-slot:toolbar>
      <div class="btn-class">
        <el-button type="primary" size="small" @click="newTask">
          新建任务
        </el-button>
        <el-button type="danger" size="small" @click="delMoreGroup">
          删除
        </el-button>
      </div>
    </template>
    <!--操作-->
    <template v-slot:action="{ row }">
      <div class="flex-around">
        <el-link
          type="primary"
          :underline="false"
          @click.stop="lookDetail(row)"
        >
          查看数据
        </el-link>
        <!-- :disabled="true"-->
        <el-link
          type="primary"
          :underline="false"
          @click="prizeDrawer(row)"
        >
          发布作品
        </el-link>
        <el-link
          type="primary"
          :underline="false"
          @click.stop="dataDetail(row)"
        >
          数据统计
        </el-link>
        <el-link
          type="danger"
          :underline="false"
          @click.stop="delSingle(row)"
        >
          删除
        </el-link>
      </div>
    </template>
    <!--数据统计-->
    <data-detail ref="detail" :visible.sync="detailShow"></data-detail>
    <!--查看详情-->
    <look-detail ref="check" :visible.sync="isLook" ></look-detail>
    <!--发布作品-->
    <publish-works ref="publish" :visible.sync="publishShow" ></publish-works>
    <!--新建任务-->
    <create-task :visible.sync="drawer" ref="releases" @load="reload"></create-task>
  </ele-pro-table>
</template>

<script>
  import CreateTask from "@/views/digitalHumanManagement/imageDisplay/components/create-task";
  import { areaList } from "@/api/matrixManage/groupManage";
  import dataDetail from '../components/data-detail.vue'
  import {getActivity,deleteActivity} from "@/api/explosiveCode/releaseActivity";
  import {queryTaskDetail, queryTaskPage,delTask} from "@/api/digitalHumanManagement/taskList";
  import LookDetail from "@/views/digitalHumanManagement/taskList/components/look-detail";
  import PublishWorks from "@/views/digitalHumanManagement/taskList/components/publish-works";
  import {accountQuery} from "@/api/matrixManage/accountList";
  import {getAiList} from "@/api/digitalHumanManagement/imageDisplay";
  export default {
    components:{PublishWorks, LookDetail, dataDetail, CreateTask},
    props: {
      // 表格数据
      activityList:Array | Object
    },
    data() {
      return {
        isLook:false,
        publishShow:false,
        drawer:false,
        detailShow:false,
        ipLocation:'',
        // 当前编辑数据
        current: null,
        // 表格列配置
        columns: [
          {
            columnKey: 'selection',
            type: 'selection',
            width: 45,
            align: 'center'
          },
          {
            prop: 'taskName',
            label: '任务名称',
            align: 'center',
            width:209,
            showOverflowTooltip: true
          },
          {
            prop: 'imgName',
            label: '使用形象 ',
            align: 'center',
            showOverflowTooltip: true
          },
          {
            prop: 'numberOfWorks',
            label: '发布作品 ',
            align: 'center',
            showOverflowTooltip: true
          },
          {
            prop: 'viewCount',
            label: '曝光量 ',
            align: 'center',
            showOverflowTooltip: true,
          },
          {
            prop: 'likeCount',
            label: '点赞数 ',
            align: 'center',
            showOverflowTooltip: true,
          },
          {
            prop: 'commentCount',
            label: '评论数 ',
            align: 'center',
            showOverflowTooltip: true
          },

          {
            prop: 'createTime',
            label: '创建时间',
            align: 'center',
            showOverflowTooltip: true
          },
          {
            label: '操作',
            columnKey: 'action',
            width: 412,
            align: 'center',
            resizable: false,
            slot: 'action',
            hideInSetting: true,
            showOverflowTooltip: true,
            fixed: 'right'
          }
        ],
        // 表格选中数据
        selection: [],
        // IP属地
        areaRegion:[],
        // 表单提交状态
        loading: false,
        // 表单数据
        form: {
          groupType:1
        },
        // 表单验证规则
        rules: {
          groupName: [
            {
              required: true,
              message: '请输入分组名称',
              trigger: 'blur'
            }
          ],
          ipLocation: [
            {
              required: true,
              message: '请选择IP属地',
              trigger: 'blur'
            }
          ]
        },
        // 数字人AI人声list
        aiList:[]
      };
    },
    created(){
      this.initAiList()
      this.init()
    },
    methods: {
      /* 刷新————表格数据源 */
      /*datasource({ page, limit, where, order }) {
        return queryTaskPage({ ...where, ...order, page, limit,...this.activityList,
          merchantId: this.$store.state.user.info.merchantId  // 商家id
        });
      },*/

      /* 如果想在数据源里面获取到接口返回的数据, 增加 async 和 await */
      async datasource({ page, limit, where, order }) {
        const result = await queryTaskPage({ ...where, ...order, page, limit,...this.activityList,
          merchantId: this.$store.state.user.info.merchantId  // 商家id
          });
        console.log("result============", result)
        let list = result.list
        if(list.length>0){
          let info={
            viewCount:0,
            likeCount:0,
            commentCount:0,
            releaseCount:0,
          }
          for(let i=0;i<list.length;i++){
            console.log("list=========",list[i].viewCount,list[i].likeCount,list[i].commentCount,list[i].numberOfWorks)
            info.viewCount += Number(list[i].viewCount) //曝光量
            info.likeCount += Number(list[i].likeCount) //点赞数
            info.commentCount += Number(list[i].commentCount) //评论数
            info.releaseCount += Number(list[i].numberOfWorks) //发布总数
          }
          info.allCount = result.count  //评论数
          console.log('info========',info,result.count)
          this.$emit('infoAll',info)
        }

        return result;  // 这里返回的数据要包含 list 和 count 两个字段，字段名称可以通过 response 属性配置
      },
      /* 刷新表格 */
      reload(where) {
        this.$refs.table.reload({ page: 1, where: where });
      },
      // 数据统计
      dataDetail(row){
        console.log('数据统计',row)
        this.detailShow=true
        this.$nextTick(()=>{
          this.$refs['detail'].query(row)
        })
      },
      // 选择ip属地
      ipChange(e){
        this.form.ipLocation=this.areaRegion[e].areaName
        this.form.area=this.areaRegion[e].areaCode
        console.log('选择ip属地e',e,'this.form',this.form)
      },
      initAiList(){
        getAiList()
          .then((data) => {
            console.log("ai人声获取data",data)
            this.aiList=data
          })
      },
      init(){
        //获取IP属地
        areaList('')
          .then((data) => {
            this.areaRegion=data.data
            console.log(' 获取IP属地--', this.areaRegion)
          })

      },

      // 新建任务
      newTask(){
        this.drawer=true
        this.$nextTick(()=>{
          this.$refs['releases'].getAiList(this.aiList)
        })
      },

      // 删除
      delMoreGroup(){
        if (!this.selection.length) {
          // 请选择需要操作的数据
          this.$message.error('请选择数据');
          return;
        }
        var ids=this.selection.map(item => {
          return item.id
        })
        console.log('删除点击事件his.selection===', this.selection ,'ids',ids)
        this.delGroups(ids)
      },
      /* 查看详情 */
      lookDetail (row) {
        this.current = row;  // 表格的行数据赋值给 current
        this.isLook=true
        this.$nextTick(()=>{
          this.$refs['check'].show(row)
        })

      },
      // 发布作品
      prizeDrawer(row){
        // 查看数字人发布详情——finishProducts.length>0 ,&& finishProducts下的对象，至少有一个status==3，才可以打开发布作品
        queryTaskDetail({
          id:row.id
        })
          .then((data) => {
            console.log('查询形象详情',data)
            let list = data.finishProducts.length >0 ? data.finishProducts : []
            if(list.length>0){
              var result = list.some(function (item) {
                if(item.status==3){
                  return true
                }
              })
              console.log('列表是否有可发布视频====',result) //true/false
              if(result){
                this.publishShow=true
                this.$nextTick(()=>{
                  this.$refs['publish'].show(row,row.id)
                })
              }
              else{
                this.$message.error('当前任务暂无视频发布！')
              }
            }
            else{
              this.$message.error('当前任务暂无视频发布！')
            }




          })
          .catch((e) => {
            this.$message.error(e);
          });
      },
      /* 删除 */
      delSingle(row) {
        let ids=[]
        ids.push(row.id)
        console.log("ids====",ids)
        this.delGroups(ids)
      },
      delGroups(ids){
        this.$confirm('此操作将永久删除该任务, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          delTask({
            ids:ids
          })
            .then((data) => {
              this.reload()
              this.$message.success('删除成功!');
            })
            .catch((e) => {
              this.$message.error(e);
            });
        }).catch(() => {
          this.$message.info('已取消删除');
        });
      }
    }
  };
</script>

<style scoped lang="less">
  /*表格头部按钮*/
  .btn-class{
    margin: 10px 0;
    .el-button{
      padding: 0;
      width: 94px;
      border-radius: 2px;
      height: 33px;
    }
  }
  /*IP属地背景*/
  .ip-bj{
    background: #DDF0FF;
    border-radius: 2px;
    height: 25px;
    line-height: 25px;
    color: #1E6CEB;
    padding:0 8px;
    display: inline-block;
  }
</style>
